
function setscreen(){
	if($(window).width()>=640){
		var viewport = document.querySelector("meta[name=viewport]");
		viewport.setAttribute('content', 'width=640,user-scalable=0,minimal-ui');
	}else{
		var viewport = document.querySelector("meta[name=viewport]");
		viewport.setAttribute('content', 'width=device-width,initial-scale=1.0,user-scalable=0,minimal-ui');
	}
}
setscreen();
$(function(){ setscreen(); });	


//计时效果
function show_time(id,date,name) 
				{
	var endtime=new Date(date);                 
	var nowtime = new Date(); 
	var leftsecond=parseInt((endtime.getTime()-nowtime.getTime())/1000); 
	m=parseInt((leftsecond/60)%60); 
	if(m<10){
		m='0'+m.toString();
	}
	s=parseInt(leftsecond%60); 
	if(s<10){
		s='0'+s.toString();
	}
	var c=new Date();
	var q=parseInt((c.getMilliseconds())/10);
	if(q<10){
		q='0'+q.toString();
	}
 	document.getElementById(id).innerHTML="<span>"+m+"</span>"+"<i>:</i>"+"<span>"+s+"</span>"+"<i>:</i>"+"<span>"+q+"</span>"; 
 	if(leftsecond<=0){ 
	 	clearInterval(name); 

	} 	
} 
function interval(name,id,date){
	name=setInterval(function(){ 
		show_time(id,date,name)
	}, 10); 
}

$(function(){
	
	//数字放大动画
	$('.buycount .ctrl').each(function(n){
		$(this).click(function(){
			if(!($(this).hasClass('disabled'))){
				$(this).parent().find('input').addClass('tobig');
				var that  = $(this);
				setTimeout(function(){
					that.parent().find('input').removeClass('tobig');
				},501)
			}
			
		}) 
	})
	//添加数量控件
	$(".buycount-ctrl").each(function(index,el){
	    var $con = $(el);
	    var $myInput = $con.find('input');
	    var max_num = parseFloat($myInput.parent().attr('max_num'));
	    $con.find('.minus').on('click',function(e){
	        var val = $myInput.val();
	        if(val <=1){
	            $myInput.val(1);
	        }else{
	            $myInput.val(--val);
	        }
	        e.preventDefault();
	        e.stopPropagation();


	        render();
	    });
	    $con.find('.plus').on('click',function(e){
	        var val = $myInput.val();
	        if(val < 1){
	            $myInput.val(1);
	        }else{
	            $myInput.val(parseFloat(val)+1>max_num ? max_num : ++val);
	        }
	        e.preventDefault();
	        e.stopPropagation();

	        render();
	    });

	    $myInput.on('blur',function(){
	    	var num = parseFloat($myInput.val()).toFixed(0);
	        if(num <= 1 || num=='NaN'){
	            $myInput.val(1);
	        }else{
	        	var num_old = $myInput.parent().attr('current_num') ? $myInput.parent().attr('current_num') : $myInput.parent().attr('max_num');
	        	$myInput.val(num>max_num ? num_old : num);
	        }
	        render();

	    });

	    function render(){
	        var s_price = $con.parent().parent().parent().find('[s_price]'),
	            $total = $con.parent().parent().parent().find('.buycount_t');

	        if(s_price.length > 0 && $total.length > 0){
	            s_price = parseFloat(s_price.attr('s_price'));
	            $total.text((s_price * $myInput.val()).toFixed(2));
	            var money_total = 0;
	            $(".buycount-ctrl").each(function(){
	            	if($(this).parent().parent().parent().find('.checked').length>0)
	            		money_total+=parseFloat($(this).parent().parent().parent().find('.buycount_t').text());
	            })
	            $('.buycount_total').text(money_total.toFixed(2));
	        }

	        if($myInput.val() <= 1){
	            $con.find('.minus').addClass('disabled');
	        }else if($myInput.val()>=max_num){
	            $con.find('.plus').addClass('disabled');
	        }else{
	            $con.find('.minus').removeClass('disabled');
	            $con.find('.plus').removeClass('disabled');
	        }
	    }
	    render();

	});
})
